<HTML><HEAD>
<!--
    -----------
    Color Maker
    -----------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)2000 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/

//------------------RANDOM NUMBERS----------------------------
var js_mult1=3141
var js_mult2=5821
var js_m1=100000000
var js_m2=10000
var js_iseed=0
var js_iseed1=0
var js_iseed2=0


// Return a Random Integer between 0 and N-1
function random(n)
{
    if (js_iseed == 0)
    {
        now = new Date()
        js_iseed = now.getHours() + now.getMinutes() * 60 
                    + now.getSeconds() * 3600
    }
    js_iseed1 = js_iseed / js_m2
    js_iseed2 = js_iseed % js_m2
    var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) * 
                js_m2 + (js_iseed2 * js_mult2)) % js_m1
    js_iseed = (tmp + 1) % js_m1
    return (Math.floor((js_iseed/js_m1) * n))
}


// --------------------Hexadecimal Conversion---------------------

// convert a single digit (0 - 16) into hex
function enHex(aDigit)
{
    return("0123456789ABCDEF".substring(aDigit, aDigit+1))
}

// convert a hex digit into decimal
function deHex(aDigit)
{
    return("0123456789ABCDEF".indexOf(aDigit))
}

// Convert a 24bit number to hex
function toHex(n)
{
    return (enHex((0xf00000 & n) >> 20) +
            enHex((0x0f0000 & n) >> 16) +
            enHex((0x00f000 & n) >> 12) +
            enHex((0x000f00 & n) >>  8) +
            enHex((0x0000f0 & n) >>  4) +
            enHex((0x00000f & n) >>  0))
}

// Convert a six character hex to decimal
function toDecimal(hexNum)
{
    var tmp = ""+hexNum.toUpperCase()
    while (tmp.length < 6) tmp = "0"+tmp
    
    return ((deHex(tmp.substring(0,1)) << 20) +
            (deHex(tmp.substring(1,2)) << 16) + 
            (deHex(tmp.substring(2,3)) << 12) +
            (deHex(tmp.substring(3,4)) << 8) +
            (deHex(tmp.substring(4,5)) << 4) +
            (deHex(tmp.substring(5,6))))
}

// --------------------JSCcolor Object---------------------

// Returns a JavaScript integer representing a JSCcolor object's color
function rawColor()
{
    return (this.red << 16) + (this.green << 8) + this.blue
}

// Returns a hex string representing a JSCcolor object's color
function hexColor()
{
    return toHex(this.rawColor())
}

// Set the JSCcolor object's color to that in a hex string.
// This routine does not add the Ox prefix.
function setColor(aString)
{
    var tmp = toDecimal(aString)

    this.red = (0xff0000 & tmp) >> 16
    this.green = (0xff00 & tmp) >> 8
    this.blue = (0xff & tmp)
    
    return this
}

// Set the JSCcolor object's color to an integer
function setDecimalColor(aNumber)
{
    var tmp = toHex(aNumber)

    this.red = eval('0x'+tmp.substring(0,2))
    this.green = eval('0x'+tmp.substring(2,4))
    this.blue = eval('0x'+tmp.substring(4,8))
    
    return this
}

// Adjust a JSCcolor object's color (red, green or blue) by an offset
function adjustColor(aColor, anOffset)
{
    this[aColor] += anOffset
    if (this[aColor] > 0xff) this[aColor] = 0xff
    if (this[aColor] < 0x0) this[aColor] = 0x0
}

// Adjust one or more of a JSCcolor object's colors by an offset
function adjustColors(colString, anOffset)
{
    var cs = colString.toUpperCase()
    
    if (cs.indexOf('R') != -1) this.adjustColor('red', anOffset)
    if (cs.indexOf('G') != -1) this.adjustColor('green', anOffset)
    if (cs.indexOf('B') != -1) this.adjustColor('blue', anOffset)
}

// JSCcolor object constructor
function JSCcolor(r, g, b)
{
    // set properties
    this.red = r
    this.green = g
    this.blue = b
    
    // set methods
    this.rawColor = rawColor
    this.hexColor = hexColor
    this.adjustColor = adjustColor
    this.adjustColors = adjustColors
    this.setColor = setColor
    this.setDecimalColor = setDecimalColor

    return this
}

// --------------------Kaleidoscope Functions---------------------

// This color object will store our current color
var myColor = new JSCcolor(0x80, 0x80, 0x80)
var whichWin = 0

// Do the actual adjustment
function doAdjust(aColor, anOffset)
{
    myColor.adjustColor(aColor, anOffset)
    
    // select a window to update
    eval("parent.JCcolorView" + whichWin +
          ".document.bgColor = myColor.hexColor()")
        
    // update chase window iteratively
    whichWin = (whichWin + 1) % 4
}

function startColor()
{
    // pick a color component to vary
    var which = random(3) + 1
    
    // select a direction of varience
    var direction = ((random(2) == 1) ? -16 : 16)
    
    // and the amount
    var amount = random(8)
    
    
    var theColor
    
    if (which == 1) theColor = 'red'
    if (which == 2) theColor = 'green'
    if (which == 3) theColor = 'blue'
    
    // do the adjustment
    for (var i = 0; i < amount; i++) doAdjust(theColor, direction)
    
    // set the next timeout
    JSCTimeOutID = window.setTimeout('startColor()', 50)
}

<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
    onload="JSCTimeOutID = window.setTimeout('startColor()',500);">
    
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/SPICE.JPG" WIDTH=37 HEIGHT=72
ALIGN = LEFT>JavaScript Groovy Color Thing II</H1></FONT>

<BLOCKQUOTE><FONT SIZE=4>
    This applet chases random color sequences around the top four windows.
    The windows are named JCcolorView1, JCcolorView2, etc. to ease
    iterative reference to each frame as shown below in 
    <FONT COLOR="770000">doAdjust()</FONT>.

<CENTER><FORM>
    <INPUT TYPE="BUTTON" VALUE="Turn Off Color Animation"
        onClick="clearTimeout(JSCTimeOutID)">
    <INPUT TYPE="BUTTON" VALUE="Restart Color Animation"
        onClick="startColor()">
</FORM></CENTER>

</FONT></BLOCKQUOTE>

<PRE><FONT COLOR="770000">
// Do the actual adjustment
function doAdjust(aColor, anOffset)
{
    myColor.adjustColor(aColor, anOffset)
    
    // select a window to update
    eval("parent.JCcolorView" + whichWin +
          ".document.bgColor = myColor.hexColor()")
        
    // update chase window iteratively
    whichWin = (whichWin + 1) % 4
}
</FONT></PRE>
</FONT></BLOCKQUOTE>


<h5>Copyright &copy;1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>